<?php

namespace App\Http\Requests;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;

class ManagerRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'username'              => 'required|min:5|max:20|unique:manager,username',
            'name'                  => 'required',
            'mobile'                => 'required|regex:/^1[345789][0-9]{9}$/',
            'email'                 => 'required|email',
            'gender'                => 'required',
            'password'              => 'required|min:6|confirmed',
            'password_confirmation' => 'same:password',
        ];
    }

    public function messages()
    {
        return [
            'username.required'  => '用户名不可以为空',
            'username.min'       => '用户名最少5位',
            'username.max'       => '用户名最多20位',
            'name.required'      => '姓名不可以为空',
            'mobile.required'    => '手机号码不可以为空',
            'mobile.regex'       => '请填写正确的手机号码',
            'email.required'     => '邮箱不可以为空',
            'email.email'        => '请填写正确的邮箱地址',
            'status.required'    => '性别不可以为空',
            'password.required'  => '密码不可以为空',
            'password.confirmed' => '密码两次输入不一致',
        ];
    }

    public function failedValidation(Validator $validator)
    {
        throw (new HttpResponseException(response()->json([
            'message' => $validator->errors()->first(),
        ], 422)));
    }

}
